var ns, WXPhotoPicker;
var app = new Vue({
    el: '#app',
    data: {
        ProjectID:0,
        form: {
            ID: 0,
            CheckUserID:'',
            CheckUserName:'',
            CompleteContent: '',
            CheckDesc:'',//验收说明
            Status:1,//验收状态 1-通过 2-不通过
        },
        imglist: [],
        maxuploadcount: CONFIG.maxuploadcount,
        controltype:1,//1-整改完成，2-验收
    },
    methods: {
        do_save: function() {
            var that = this;
            if (that.form.IsAllowUpload && that.imglist.length == 0) {
                api.toast({
                    msg: '请上传图片',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if(that.controltype==1){
                that.do_save_process();
            }else{
                that.do_save_yanshou();
            }
            
        },
        reload_list: function() {
          api.sendEvent({
              name: 'reloadservicelist',
          });
          api.sendEvent({
              name: 'updata_task_con',
          });
        },
        do_save_process: function() {
            var that = this;
            var options = {};
            options.action = "completequalitycontrol";
            options.ID = that.form.ID;
            options.ProcessType=2;//1-处理，2-完工
            options.CheckUserID = that.form.CheckUserID;
            options.CheckUserName = that.form.CheckUserName;
            options.CompleteContent = that.form.CompleteContent;
            var filelist = [];
            for (var i = 0; i < that.imglist.length; i++) {
                if (that.imglist[i].isupload) {
                    filelist.push(that.imglist[i].url);
                }
            }
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    api.toast({
                        msg: '操作成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    that.reload_list();
                    setTimeout(function() {
                        api.closeWin();
                    }, 1000)
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                files: {
                    file: filelist
                }
            });
        },
        //验收
        do_save_yanshou:function(){
            var that = this;
            var options = {};
            options.action = "checkquality";
            options.ID = that.form.ID;
            options.CheckUserID = that.form.CheckUserID;
            options.CheckUserName = that.form.CheckUserName;
            options.Type = that.form.Status;
            options.CheckDesc = that.form.CheckDesc;
            var filelist = [];
            for (var i = 0; i < that.imglist.length; i++) {
                if (that.imglist[i].isupload) {
                    filelist.push(that.imglist[i].url);
                }
            }
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    api.toast({
                        msg: '操作成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    that.reload_list();
                    setTimeout(function() {
                        api.closeWin();
                    }, 1000)
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                files: {
                    file: filelist
                }
            });
        },
        do_select_area:function(status){
            var that=this;
            that.form.Status=status;
        },
        //验收人
        do_choose_user:function(){
            var that=this;
            ns.openWin('quality_chooseperson', '选择验收人', {
                ProjectID: that.ProjectID,
                type:2,
                id:that.form.ID,
            });
              //通用
            // var projectIds = [];
            // if (that.ProjectID) {
            //     projectIds.push(that.ProjectID);
            // }
            // ns.openWin('chooseuser_frm', '选择验收人', {
            //     source: 'chooseuser',
            //     loadType: 2,
            //     ProjectIDs: JSON.stringify(projectIds),
            // });
        },
        choose_picture: function() {
            var that = this;
            ns.confirmPer('camera', function() {
                that.choose_picture_start();
            })
        },
        choose_picture_start:function(){
            var that = this;
            if (that.imglist.length >= that.maxuploadcount) {
                api.toast({
                    msg: '最多支持' + that.maxuploadcount + '张图片',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            api.actionSheet({
                cancelTitle: '取消',
                buttons: ['拍照', '从相册选择']
            }, function(ret, err) {
                //拍照
                if(ret.buttonIndex==1){ 
                    that.upload_choose_picture();
                    return;
                }
                //从相册选择
                if(ret.buttonIndex==2){
                    that.choose_picture_album();
                    return;
                } 
                //取消 
                else{
                    return false;  
                }  
            });
        },
        upload_choose_picture:function(){
            var that = this;
            api.getPicture({
                sourceType: 'camera',
                encodingType: 'jpg',
                destinationType: 'url',
                allowEdit: false,
                saveToPhotoAlbum: true,
            }, function(ret, err) {
                if (ret&&ret.data) {
                    that.do_imageFilter(ret.data);
                    // that.imglist.push({
                    //     ID: 0,
                    //     url: ret.data,
                    //     thumbPath: ret.data,
                    //     isupload: true
                    // });
                } 
            });
        },
        choose_picture_album:function(){
            var that = this;
            WXPhotoPicker.open({
                max: that.maxuploadcount - that.imglist.length,
                type:'image',
            }, function(ret) {
                if (ret) {
                    if (ret.list.length > 0) {
                        ret.list.forEach((item) => {
                            that.do_imageFilter(item.path);
                            // that.imglist.push({
                            //     ID: 0,
                            //     url: item.path,
                            //     thumbPath: item.path,
                            //     isupload: true
                            // });
                        });
                    }
                }
            });
        },
         //图片压缩
         do_imageFilter:function(path){
            var that=this;
            var imgName = Date.now() + '.jpg';
            var target_w = 500;
            var imageFilter = api.require('imageFilter');
            imageFilter.getAttr({
                path: path
            }, function(ret, err) {
                if (ret.status) {
                    if (ret.width < target_w) {
                        target_w = ret.width;
                    }
                    var original_w = ret.width;
                    var original_h = ret.height;
                    var scale = original_w / target_w;
                    var target_h = Math.round(original_h / scale);
                    imageFilter.compress({
                        img: path,
                        quality: 1,
                        size: {
                            w: target_w,
                            h: target_h
                        },
                        save: {
                            album: false,
                            imgPath: api.cacheDir + '/upload/',
                            imgName: imgName
                        }
                    }, function(ret, err) {
                        if (ret.status) {
                            var imgFullName=api.cacheDir + '/upload/' + imgName;
                            that.imglist.push({
                                ID: 0,
                                url: imgFullName,
                                thumbPath: imgFullName,
                                isupload: true
                            });
                        }else{
                            alert( JSON.stringify( err ) );
                        }
                    });
                }
            });
        },
        remove_img: function (index,item) {
            var that = this;
            that.imglist.splice(index, 1);
        },
        do_viewimages: function(activeIndex) {
            var that = this;
            var imageUrls = [];
            for (var i = 0; i < that.imglist.length; i++) {
                imageUrls.push(that.imglist[i].url);
            }
            var imageBrowser = api.require('imageBrowser');
            imageBrowser.openImages({
                showList: false,
                activeIndex: activeIndex,
                imageUrls: imageUrls
            });
        },
    }
});
apiready = function() {
    api.parseTapmode();
    WXPhotoPicker = api.require('WXPhotoPicker');
    ns = window.Foresight.Util;
    app.ProjectID = api.pageParam.ProjectID || 0;
    app.form.ID = api.pageParam.ID || 0;
    //1-整改完成，2-验收
    app.controltype=api.pageParam.type || 0;
    if(app.controltype==2){
        app.form.CheckUserID = api.pageParam.CheckUserID || 0;
        app.form.CheckUserName = api.pageParam.CheckUserName;
    }

    api.addEventListener({
        name: 'do_qualitychoose'
    }, function (ret, err) {
        if (ret) {
            if(ret.value.type==2){
                app.form.CheckUserName=ret.value.item.FinalRealName;
                app.form.CheckUserID=ret.value.item.FinalUserID;
            }
            
        }
    });

    api.addEventListener({
        name: 'purchase_add'
    }, function (ret) {
        var chooselist = ret.value.data;
            if (chooselist.length > 0) {
                app.form.CheckUserID=chooselist[0].UserID;
                app.form.CheckUserName=chooselist[0].UserName;
            }
    })
    // app.type = api.pageParam.type;
}
